package com.senlingspace.web.controller.douyin;


import com.alibaba.fastjson2.JSON;
import com.senlingspace.common.core.domain.AjaxResult;
import com.senlingspace.common.exception.ServiceException;
import com.senlingspace.payment.douyin.DouyinHttpClient;
import com.senlingspace.payment.douyin.bean.request.DouyinPreVerifyTicketRequest;
import com.senlingspace.payment.douyin.bean.request.DouyinProductRequest;
import com.senlingspace.payment.douyin.bean.response.DouyinProductResponse;
import com.senlingspace.payment.douyin.service.DouYinProductService;
import com.senlingspace.payment.utils.DouyinSignUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.rmi.ServerException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
 * 抖音商品控制器
 */

@Slf4j
@Validated
@AllArgsConstructor
@RestController
@RequestMapping(path = "/douyin/commodity/v1")
public class DouYinProductController {


    private DouYinProductService douYinProductService;


    /**
     * 抖音 添加修改商品
     */
    @PostMapping(path = "/create")
    public AjaxResult create( Long shopId) {
        try {
            ArrayList<DouyinProductResponse> invoke = douYinProductService.create(shopId);
            return AjaxResult.success(invoke);
        }catch (ServiceException e){
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 查询商品模板接口
     */
    @GetMapping(path = "/template")
    public AjaxResult prepare(@RequestBody DouyinPreVerifyTicketRequest douyinPreVerifyTicketRequest) {
        return AjaxResult.success("success");
    }


    public static void main(String[] args) {

        DouyinHttpClient.init("https://open.douyin.com", "awc3lwtq1grsb5z9", "366deff1d4bbecb25e2200a3bb9750de", null, null, null);
        DouyinSignUtil.setSecret("49d263dafbd6295c740ef8c1a5fd0369");


        DouyinProductRequest request = new DouyinProductRequest();

// 设置商家ID
        request.setAccountId("7421093720344233996");

// 创建Product对象并设置字段
        DouyinProductRequest.Product product = new DouyinProductRequest.Product();
        product.setAccountName("银河系漫游指南XR沉浸体验空间");
        product.setBizLine(1);  // 业务线
        product.setCategoryId(4017001L);  // 三级品类ID
        product.setOutId("ticket_openapi_0711_01");  // 外部商品ID
        product.setProductName("银河系漫游指南XR沉浸体验空间");  // 商品名
        product.setProductType(1);  // 商品类型
        product.setSoldStartTime(1729151330L);  // 售卖开始时间
        product.setSoldEndTime(1729237730L);  // 售卖结束时间

// 店铺信息
        DouyinProductRequest.Poi poi = new DouyinProductRequest.Poi();
        poi.setPoiId("7419905288175618100");  // poiID
        product.setPois(Arrays.asList(poi));

// 设置 attr_key_value_map
        Map<String, String> attrMap = new HashMap<>();
        attrMap.put("appointment", "{\"need_appointment\":true, \"ahead_time_type\":2, \"ahead_hour_num\":5, \"external_link\":\"urlxxx\", \"order_appointment_time_url\":\"urlxxx\"}");
        attrMap.put("auto_renew", "true");
        attrMap.put("bring_out_meal", "false");
        attrMap.put("can_no_use_date", "{\"enable\": true, \"days_of_week\": [7, 1, 2, 3, 4], \"holidays\": [1, 2, 3, 4, 5], \"date_list\": [\"2022-03-08\", \"2022-03-09\"], \"holiday_dates\": {\"1\": \"2022.01.01-2022.01.03\"}}");
        attrMap.put("description_rich_text", "[{\"note_type\": 1, \"content\": \"其他说明信息-门票\"}]");
        attrMap.put("image_list", "[{\"url\":\"https://dev.senlingspace.com/res/file/2024/10/17/2_20240812195040A020_20241017154423A002.jpg\"}]");
        attrMap.put("use_time", "{\"use_time_type\":1}");
        attrMap.put("show_channel", "2");
        attrMap.put("RefundPolicy", "2");
        attrMap.put("rec_person_num", "10");
        attrMap.put("rec_person_num_max", "50");
        attrMap.put("Notification", "[{\"title\":\"标题\",\"content\":\"银河系旅游指南\"}]");
        attrMap.put("use_date", "{\"use_date_type\":1,\"use_start_date\":\"2024-10-17\",\"use_end_date\":\"2024-11-18\"}");
        attrMap.put("Description", "[\"\"]");
        product.setAttrKeyValueMap(attrMap);

        request.setProduct(product);

// 创建SkuStruct对象并设置字段
        DouyinProductRequest.SkuStruct sku = new DouyinProductRequest.SkuStruct();
        sku.setActualAmount(999);  // 实际支付价格，单位：分
        sku.setOriginAmount(29990);  // 原价，单位：分
        sku.setSkuName("成人票");  // SKU 名称
        sku.setStatus(1);  // 状态，1 为在线

// 库存信息
        DouyinProductRequest.StockStruct stock = new DouyinProductRequest.StockStruct();
        stock.setLimitType(1);  // 库存上限类型，1：有限库存
        stock.setStockQty(1000);  // 总库存数量
        sku.setStock(stock);

// 设置 sku 的 attr_key_value_map
        Map<String, String> skuAttrMap = new HashMap<>();
        skuAttrMap.put("code_source_type", "1");
        skuAttrMap.put("settle_type", "1");
        skuAttrMap.put("use_type", "1");
        skuAttrMap.put("commodity", "[{\"group_name\":\"成人票\",\"total_count\":1,\"option_count\":1,\"item_list\":[{\"name\":\"门票\",\"price\":1999,\"count\":1,\"unit\":\"张\"}]}]");
        skuAttrMap.put("limit_rule", "{\"is_limit\":true,\"total_buy_num\":5}");
        sku.setAttrKeyValueMap(skuAttrMap);
        request.setSku(sku);

// 打印封装好的请求体
        System.out.println(JSON.toJSONString(request));

        DouyinProductResponse invoke2 = DouyinHttpClient.invoke(request);
        System.out.println(invoke2);


//
//        DouyinGoodsTemplateRequest douyinGoodsTemplateRequest1 = new DouyinGoodsTemplateRequest();
//        douyinGoodsTemplateRequest1.setCategoryId(1 + "");
//        douyinGoodsTemplateRequest1.setProductType(5 + "");
//
//
//        DouyinGoodsTemplateResponse invoke1 = DouyinHttpClient.invoke(douyinGoodsTemplateRequest1);
//        System.out.println(invoke1);
//
//
////        添加商品
//        DouyinProductRequest douyinGoodsTemplateRequest = new DouyinProductRequest();
//
//        DouyinProductRequest.Product product = new DouyinProductRequest.Product();
//        product.setOutId("1");
//        product.setProductName("测试商品");
//        product.setCategoryFullName("全名");
//        product.setCategoryId(1l);
//        product.setProductType(5);
//        /**
//         * 业务线。（更新商品时不可修改）
//         * •1：闭环自研开发者（如酒旅预售券）
//         * •5：小程序
//         * */
//        product.setBizLine(1);
//        product.setAccountName("银河系漫游指南XR沉浸体验空间");
//
////      todo  poiID，技术服务商保存商品填写该字段(在来客的门店管理列表中)
//        DouyinProductRequest.Poi poi = new DouyinProductRequest.Poi();
//        poi.setPoiId(String.valueOf(1));
//
//        product.setPois(java.util.Arrays.asList(poi));
//
//
//        douyinGoodsTemplateRequest.setProduct(product);
//
//        DouyinProductRequest.SkuStruct sku = new DouyinProductRequest.SkuStruct();
//        sku.setSkuName("测试SKU名称");
//        sku.setOriginAmount(10);
//        sku.setActualAmount(1);
//        DouyinProductRequest.StockStruct stockStruct = new DouyinProductRequest.StockStruct();
//        stockStruct.setLimitType(2);
//        sku.setStock(stockStruct);
//
//        douyinGoodsTemplateRequest.setSku(sku);
//        douyinGoodsTemplateRequest.setAccountId("7421093720344233996");
//
//
//        DouyinProductResponse invoke = DouyinHttpClient.invoke(douyinGoodsTemplateRequest);
//
//        System.out.println(JSON.toJSONString(invoke));

    }


}
